package com.yimi.yinhepda.views.login.util;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Handler;
import android.os.Looper;

import com.pda.uce.commom.util.LoadUtils;
import com.pda.uce.commom.util.LogUtil;
import com.pda.uce.commom.util.MyDialog;
import com.pda.ym.callbacks.UICallBack;
import com.pda.ym.entity.response.GetVersionResponse;
import com.pda.ym.net.NetRequest;
import com.pda.ym.utils.ActivityStack;
import com.pda.ym.utils.Constants;
import com.yimi.yinhepda.R;
import com.yimi.yinhepda.utils.OkHttpDownUtil;
import com.yimi.yinhepda.utils.ToastUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: lj
 * Date: 2020/7/28 14:25
 * Description:
 */
public class LoginUtil {

    /**
     * 查询db信息，获取下载url
     *
     * @param saveDir  zip保存目录
     * @param fileName zip文件名称
     */
    public static void queryDBInfo(final Context context, final String saveDir, final String fileName,
                                   final UICallBack callBack) {
        Map<String, String> params = new HashMap<>(3);
        params.put("pluginCode", "all");
        params.put("platform", "yh-pad-db");
        params.put("versionCode", "0");
//        params.put("userCode", sharedPreferUtil.getValue(Constants.USER_CODE));
        LoadUtils.showLoading("查询中...", context);
        new NetRequest().setMethod(NetRequest.Method.GET)
                .setUrl(Constants.UPDATE_INFO_SERVER_URL)
                .setParams(params)
                .setResponsClazz(GetVersionResponse.class)
                .execute(new UICallBack() {
                    @Override
                    public void onError(String msg) {
                        LoadUtils.hideLoading(context);
                        ToastUtil.showToast(msg);
                        callBack.onError(msg);
                    }

                    @Override
                    public void onSuccess(Object obj) {
                        GetVersionResponse response = (GetVersionResponse) obj;
                        if (response.getData() != null) {
                            startDownLoadZip(context, response.getData().getDownloadUrl(), saveDir, fileName, callBack);
                        } else {
                            LoadUtils.hideLoading(context);
                            ToastUtil.showToast("查询失败！");
                            callBack.onError("查询失败！");
                        }
                    }
                });
    }

    /**
     * 开始下载zip文件
     *
     * @param url      下载的url
     * @param saveDir  下载保存目录
     * @param fileName 下载保存文件名称
     */
    private static void startDownLoadZip(final Context context, final String url, final String saveDir, final String fileName,
                                         final UICallBack callBack) {

        OkHttpDownUtil.get().download(url, saveDir, fileName, new OkHttpDownUtil.OnDownloadListener() {
            @Override
            public void onDownloadSuccess() {
                callBack.onSuccess(null);
            }

            @Override
            public void onDownloading(final int progress) {
                new Handler(Looper.getMainLooper()).post(new Runnable() {
                    @Override
                    public void run() {
                        LoadUtils.showLoading("下载资料中..." + progress + "/100%", context);
                    }
                });
            }

            @Override
            public void onDownloadFailed() {
                new Handler(Looper.getMainLooper()).post(new Runnable() {
                    @Override
                    public void run() {
                        LoadUtils.hideLoading(context);
                        ToastUtil.showToast("下载失败！");
                        callBack.onError("下载失败！");
                    }
                });
            }
        });
    }

    public static void retryDialog(final Context context, final DialogInterface.OnClickListener onClickListener) {
        MyDialog dialog = MyDialog.getInstance(context);
        dialog.setIcon(R.mipmap.alert_error);
        dialog.setDialogType(MyDialog.DialogType.ALERT_DIALOG);
        dialog.setTitle("系统提示");
        dialog.setMessage("基础数据下载失败，是否重试？");
        dialog.setPositiveClickListener("重试", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                MyDialog.setNull();
                onClickListener.onClick(dialog, which);
            }
        });
        dialog.setNegativeClickListener(R.drawable.btn_login_selector, "退出", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                MyDialog.setNull();
                ActivityStack.getInstance().exit();
                ((Activity) context).finish();
            }
        });
        dialog.setCancelable(false);
        dialog.setCanceledOnTouchOutside(false);

        try {
            dialog.show();
        } catch (Exception e) {
            LogUtil.e(e.toString());
        }
    }
}
